/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.view;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

/**
 *
 * @author admin
 */
public class PanelTambahFotoAnggota extends JPanel {

    private JLabel labelKodeAnggota;
    private JTextField textKodeAnggota;
    private JButton tombolSimpan;
    private JButton tombolCariGambar;
    private File file;
    private JPanel panelTombol;
    private BufferedImage image;

    public PanelTambahFotoAnggota() {
        inisialisasi();
        file = new File("NoPicture.jpg");
        try {
            image = ImageIO.read(file);
            image.getGraphics();
        } catch (IOException ex) {
            Logger.getLogger(PanelTambahFotoAnggota.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void inisialisasi() {
        setLayout(new BorderLayout());
        panelTombol = new JPanel();
        panelTombol.setLayout(new FlowLayout());
        labelKodeAnggota = new JLabel("Kode");
        panelTombol.add(labelKodeAnggota);
        textKodeAnggota = new JTextField();
        textKodeAnggota.setColumns(10);
        panelTombol.add(textKodeAnggota);
        tombolCariGambar = new JButton("CariGambar");
        panelTombol.add(tombolCariGambar);
        tombolSimpan = new JButton("Simpan");
        panelTombol.add(tombolSimpan);
        tombolCariGambar.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                tombolCariGambarAction();
            }
        });
        tombolSimpan.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                tombolSimpanAction();
            }
        });
        textKodeAnggota.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                textKodeAnggotaAction();
            }
        });
add(panelTombol, BorderLayout.SOUTH);
    }

    public void tombolSimpanAction() {
        FileOutputStream os = null;
        try {
            File fileBaru = new File (textKodeAnggota.getText()+".png");
            os = new FileOutputStream(fileBaru);
        } catch (FileNotFoundException ex) {
            Logger.getLogger(PanelTambahFotoAnggota.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            try {
                os.close();
            } catch (IOException ex) {
                Logger.getLogger(PanelTambahFotoAnggota.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }

    public void tombolCariGambarAction() {
        JFileChooser chooser = new JFileChooser();
        int status = chooser.showOpenDialog(this);
        if (status==JFileChooser.APPROVE_OPTION) {
        file = chooser.getSelectedFile();
        repaint();
        try {
                image = ImageIO.read(file);
            } catch (IOException ex) {
                Logger.getLogger(PanelTambahFotoAnggota.class.getName()).log(Level.SEVERE, null, ex);
            }
        image.getGraphics();
    }
    }
    
    public void textKodeAnggotaAction() {
        File fileBaru = new File (textKodeAnggota.getText()+".png");
        repaint();

    }
    
    @Override
    public void paintComponent(Graphics g) {
g.drawImage(image,
0, 0, 400, 300,
0, 0, image.getWidth(null), image.getHeight(null),
null);
}
}